/**
 * Created by yan_my on 2017/1/14.
 */
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.util.ArrayList;
import org.json.*;

public class Test
{
    public static void main(String[] args)throws  Exception
    {
        String[] tids = {"22","26","27","30","36","44","56","60","66","72","79","81","99","101","111","123","133"};
        HttpClient httpClient = HttpClients.createDefault();
        String url = "http://api.bilibili.com/archive_rank/getarchiverankbypartion?type=jsonp&tid=";

        for(String tid:tids)
        {
            url = url+tid+"&pn=";
            for(int i=1;i<1000;i++) {
                url += String.valueOf(i);
                HttpGet req = new HttpGet(url);
                req.addHeader("Accept","application/json,text/javascript,*/*;q=0.01");
                req.addHeader("Accept-Encoding", "gzip,deflate,sdch");
                req.addHeader("AcceptLanguage", "zh-CN,zh;q=0.8");
                req.addHeader("Content-Type", "text/html;charset=UTF-8");
                req.addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36 SE 2.X MetaSr 1.0");
                HttpResponse rep = httpClient.execute(req);
                HttpEntity repEntity = rep.getEntity();
                String content=EntityUtils.toString(repEntity);
                //System.out.println(content);

                JSONObject js = new JSONObject(content);
                JSONObject data = js.getJSONObject("data");
                JSONObject archives = data.getJSONObject("archives");
                JSONObject arc = archives.getJSONObject(String.valueOf(0));

                for(int j = 0;j < archives.length();j++)
                {
                   arc = archives.getJSONObject(String.valueOf(j));
                    int av_number = arc.getInt("aid");
                    String title = arc.getString("title");
                    String author = arc.getString("author");
                    String tname = arc.getString("tname");
                    int favorite = arc.getInt("favorites");
                    DataBase dataBase = new DataBase();
                    Download dowmload = new Download();
                    dataBase.insert(av_number,title,author,tname,favorite);
                    System.out.println(av_number+title+author+favorite);
                }
                //System.out.println(js);
                //System.out.println(js.getJSONObject("data"));
            }
        }
    }
    public static String getCategory(String website) throws Exception
    {
        HttpClient httpClient = HttpClients.createDefault();
        HttpGet req = new HttpGet(website);
        req.addHeader("Accept","application/json,text/javascript,*/*;q=0.01");
        req.addHeader("Accept-Encoding", "gzip,deflate,sdch");
        req.addHeader("AcceptLanguage", "zh-CN,zh;q=0.8");
        req.addHeader("Content-Type", "text/html;charset=UTF-8");
        req.addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36 SE 2.X MetaSr 1.0");
        HttpResponse rep = httpClient.execute(req);
        HttpEntity repEntity = rep.getEntity();
        String content=EntityUtils.toString(repEntity);
        JSONObject ret = new JSONObject(content);
        JSONObject result = ret.getJSONObject("data").getJSONObject("archives");
        String category = result.getJSONObject((String.valueOf(19))).getString("tname");
        if(category!=null)
        {
            return category;
        }
        return null;
    }
}
